<template>
  <el-card class="case">
    <template #header>
      <div class="card-header">
        <span>题目背景</span>
        <el-button @click="closeAnli">关闭</el-button>
      </div>
    </template>
    <div class="case-div">
      <div ref="caseRef" v-html="caseContent" class="case-content"></div>
    </div>
  </el-card>
</template>

<script>
import {nextTick, onMounted, ref, watch,getCurrentInstance} from "vue";

export default {
  name: "Case",
  props: {
    caseContent: {
      type: String
    }
  },
  setup(props, {emit}) {
    let caseImg = ref(300);
    let caseRef = ref(null);
    let closeAnli = () => {
      emit('anliClose')
    }

    //设置案例宽度
    const setCaseWidth = () => {
      nextTick(() =>{
        setTimeout(() =>{
          let imgs = caseRef.value.querySelectorAll('img');
          let arr = [];
          console.log(imgs);
          for (let item of Array.from(imgs)) {
            console.log(item.width)
            arr.push(item.width)
          }
          console.log(arr)
          caseImg.value = arr.length ? Math.max(...arr) +50 + 'px' : '35%';
        },200)
      })
    }
    onMounted(() =>{
      // nextTick(() =>{
      //   // console.log(caseRef.value.$el)
      //   console.log(caseRef.value.clientWidth)
      // })
    })
    watch(() => props.caseContent,  () => {
      console.log('案例变化');
      setCaseWidth();
    },{deep:true,immediate:true})
    return {
      caseImg,
      caseRef,
      closeAnli
    }
  }
}
</script>

<style scoped>
.case-content {
  padding-bottom: 200px;
}

/*.case-content ::v-deep(img){*/
/*  max-width: 100%;*/
/*}*/
.case-div {
  overflow: scroll;
  height: calc(100vh - 120px);
  overflow-x: hidden;
}

.case-div::-webkit-scrollbar {
  width: 4px;
  /*height: 4px;*/
}

.case-div::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}

.case-div::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);

}

.card-header {
  display: flex;
  justify-content: space-between;
}

.case {
  padding: 0 20px;
  width: v-bind(caseImg);
  height: 100vh;
  position: fixed;
  right: 0px;
  top: 0px;
  z-index: 2;
}

.case ::v-deep(.el-card__body) {
  padding: 20px 0px;
}
</style>
